
<!DOCTYPE html>
<!--
  Copyright 2011 Google Inc. All Rights Reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<html>
  <head>
    <title>BDU Tools</title>

    <style type="text/css">
      body { font-family: Arial; }
    </style>
	
   	<style type="text/css">
		.mytable {
		border:1px solid #000000;
		border-collapse:collapse;
		width:200px;
		}
		.mytable td{
		border:1px solid #000000;
		}
		.mytable th{
		background:#cccccc;
		border:1px solid #000000;
		}
	</style>
	
    <script type="text/javascript">
   	  var clientId = '863565692657.apps.googleusercontent.com';
	 var apiKey='AIzaSyCj4FjUE6NDN0wvxZ0npSfAaBjbQaYhEEo';
      	var scopes = 'https://www.googleapis.com/auth/fusiontables';
	  var tableId='1BMdb9Rn_HJ7MU4A6Tk7zNSZxVLbDHTkoWTWWe7w';	//Contact table
	  var resp1;
      // Initialize the client, set onclick listeners.
      function initialize() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(function() { auth(true); }, 1);
      }
	
	//function logOut(){
	//		document.location.href="https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://phanthanhhao26.appspot.com";
	//	}
      // Run OAuth 2.0 authorization.
      function auth(immediate) {
        gapi.auth.authorize({
          client_id: clientId,
          scope: scopes,
          immediate: immediate
        }, handleAuthResult);
      }

      // Handle the results of the OAuth 2.0 flow.
      function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
		selectData();
        }
      }
	  
      // Run a request to SELECT data.
      function selectData() {
        query('SELECT * FROM ' + tableId);
		  	
      }

      // Send an SQL query to Fusion Tables.
      function query(query) {
        var lowerCaseQuery = query.toLowerCase();
        var path = '/fusiontables/v1/query';
        var callback = function(element) {
          return function(resp) {
		  	if(resp.rows!=null){
				var nrCols=resp.columns.length;
				var nrRows=resp.rows.length;
				var root=document.getElementById('mydiv');
				var tab=document.createElement('table');
				tab.className="mytable";
				var tbo=document.createElement('tbody');
				var row, cell;
				
				for(var i=0; i<nrRows; i++){
					if (i%2==0)
						row=document.createElement('tr');
					cell=document.createElement('td');
					for(var j=1; j<nrCols; j++){
						bold=document.createElement('b');
						line =document.createElement('br');
						bold.appendChild(document.createTextNode(resp.columns[j]+' : '));
						cell.appendChild(bold);
						cell.appendChild(document.createTextNode(resp.rows[i][j]));
						cell.appendChild(line);
						row.appendChild(cell);
					}
					row.style.border = "1px solid #000";
					tbo.appendChild(row);
				}
				tab.appendChild(tbo);
				tab.style.width = "100%";
				root.appendChild(tab);
			}
			else
				alert("No data!");
          };
        }
       if (lowerCaseQuery.indexOf('select') != 0 &&
            lowerCaseQuery.indexOf('show') != 0 &&
            lowerCaseQuery.indexOf('describe') != 0) {

          var body = 'sql=' + encodeURIComponent(query);
          runClientRequest({
            path: path,
            body: body,
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': body.length
            },
            method: 'POST'
          }, callback('insert-data-output'));

        } else {
          runClientRequest({
            path: path,
            params: { 'sql': query }
          }, callback('select-data-output'));
        }
      }

      // Execute the client request.
      function runClientRequest(request, callback) {
        var restRequest = gapi.client.request(request);
        restRequest.execute(callback);
      }
    </script>
     <script src="https://apis.google.com/js/client.js?onload=initialize"></script>
      <link rel="stylesheet" type="text/css" href="../Css file/content.css">
     <link href='http://fonts.googleapis.com/css?family=PT+Sans' rel='stylesheet' type='text/css'>
  </head>
  <body>
   	 <div id="current">
       	    <h4>Homepage>Contact Information>View as Name Card</h4>
     </div>
	<div id="mydiv"></div>

  </body>
</html>
